// pages/addressList/addressList.js
import { async } from '../../lib/runtime/runtime.js';
import {request} from '../../request/index.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      uid:null,
      addressList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userinfo =wx.getStorageSync('userInfo')
    let uid =userinfo.id
    this.setData({
      uid:uid
    })
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  }, async getAddress(){
    const result =  await request({
      url: '/sites/Sites_id',
      data:{
        user_id : this.data.uid
      }
    })
    console.log(result);
   var addressList= result.data.list;

   console.log(addressList);
   this.setData({
    addressList:addressList
   })
   
  },

  toUpdate(e){
     var {aid} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/addAddress/addAddress?aid='+aid,
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  }
  ,toBack(){
    wx.navigateBack({
      delta: 1,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  toAddRess(){
    wx.navigateTo({
      url: '/pages/addAddress/addAddress',
    })
  }
  ,async onDelete(e){

   var aid = e.currentTarget.dataset.id;
   const result =  await request({
    url: '/sites/Delete',
    data:{
      id : aid
    }
  })
  console.log(result);
  if (result.code == 200) {
    Toast.success(result.message);
  }else{
    Toast.fail(result.message);
  }



  this.getAddress();
  },
  //选择地址 
  changeAddress(e){
    console.log(e);
    let {arrid} =e.currentTarget.dataset;
    // let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
    // // let prevPage = pages[ pages.length - 2 ];  
    // // console.log(result);
    //  let prevPage = pages[ pages.length - 2 ];  
    // console.log(prevPage);
    console.log(arrid);
  //   prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
  //     arrId: arrid
  // })
  wx.navigateTo({
    url: '/pages/shopping/shopping?arrId='+arrid,
  })

  }
})
